package com.sz.virtualbank.web.controller.account;

import com.sz.virtualbank.common.annotation.LoginRequired;
import com.sz.virtualbank.common.result.CommonResponse;
import com.sz.virtualbank.dao.entity.User;
import com.sz.virtualbank.service.account.AccountService;
import com.sz.virtualbank.service.user.UserService;
import com.sz.virtualbank.web.vo.account.RechargeReqVO;
import com.sz.virtualbank.web.vo.account.TransferReqVO;
import com.sz.virtualbank.web.vo.account.WithdrawReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.Map;

/**
 * 账户相关接口
 * 
 * @author Claude
 */
@RestController
@RequestMapping("/api/account")
@Slf4j
@Api(tags = "账户接口")
public class AccountController {

    @Autowired
    private AccountService accountService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 查询账户余额
     */
    @GetMapping("/balance")
    @ApiOperation("查询账户余额")
    @LoginRequired
    public CommonResponse<Map<String, BigDecimal>> getAccountBalance(HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        Map<String, BigDecimal> balanceInfo = accountService.getAccountBalance(user.getId());
        return CommonResponse.success(balanceInfo);
    }
    
    /**
     * 充值
     */
    @PostMapping("/recharge")
    @ApiOperation("充值")
    @LoginRequired
    public CommonResponse<String> recharge(@RequestBody @Valid RechargeReqVO reqVO,
                                         HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        String tradeNo = accountService.recharge(user.getId(), 
                reqVO.getAmount(), reqVO.getChannel(), reqVO.getUniqueToken());
        
        return CommonResponse.success(tradeNo);
    }
    
    /**
     * 提现
     */
    @PostMapping("/withdraw")
    @ApiOperation("提现")
    @LoginRequired
    public CommonResponse<String> withdraw(@RequestBody @Valid WithdrawReqVO reqVO, 
                                         HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        String tradeNo = accountService.withdraw(user.getId(), 
                reqVO.getAmount(), reqVO.getBankCardId(), reqVO.getPassword());
        
        return CommonResponse.success(tradeNo);
    }
    
    /**
     * 转账
     */
    @PostMapping("/transfer")
    @ApiOperation("转账")
    @LoginRequired
    public CommonResponse<String> transfer(@RequestBody @Valid TransferReqVO reqVO, 
                                         HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        String tradeNo = accountService.transfer(user.getId(), 
                reqVO.getTargetAccount(), reqVO.getAmount(), reqVO.getRemark(), reqVO.getPassword());
        
        return CommonResponse.success(tradeNo);
    }
}
